<?php
namespace App\Models\Base;

use App\Models\Model;

class DistrictModel extends Model
{

    const TABLENAME = 'tw_base_district';

    const STATUS_DEFAULT = 1;

    const STATUS_REMOVE = 0;
    
    private static $field = ['id', 'name', 'level', 'upid'];

    /**
     * 查询一条数据
     * 
     * @param int $id
     * @param array $fields
     * @return \Illuminate\Support\Collection
     */
    public static function findById($id, array $fields = [])
    {
        $query = self::dbTable();
        if (count($fields)) {
            $query->select($fields);
        }

        return $query->where('id', $id)->get();
    }
    
    /**
     * 查询列表（父 ID）
     * 
     * @param int $upid
     * @return \Illuminate\Support\Collection
     */
    public static function selectByUpid($upid)
    {
        $where[] = ['upid', $upid];
        $where[] = ['status', self::STATUS_DEFAULT];
        return self::dbTable()
            ->select(self::$field)
            ->where($where)
            ->get();
    }

    /**
     * 查询列表（省份、城市）
     *
     * @return \Illuminate\Support\Collection
     */
    public static function selectBySimplify()
    {
        $where[] = ['status', self::STATUS_DEFAULT];
        return self::dbTable()
            ->select(self::$field)
            ->whereIn('level', [1, 2])
            ->where($where)
            ->get();
    }
}
